﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Drawing;

namespace TsChanManLibrary
{
    public class cHelper
    {

        public static void SerializeToXML(object obj, string file)
        {
            SerializeToXML(obj, file, null);
        }

        public static void SerializeToXML(object obj, string file, Type type)
        {
            if (type == null) type = obj.GetType();

            XmlSerializer x = new XmlSerializer(type);
            StreamWriter sw = new StreamWriter(file);
            x.Serialize(sw, obj);
            sw.Close();
        }

        public static byte[] SerializeToXML(object obj)
        {
            MemoryStream ms = new MemoryStream();
            XmlSerializer x = new XmlSerializer(obj.GetType());
            x.Serialize(ms, obj);
            return ms.ToArray();
        }

        public static object DeserializeFromXML(Type t, string file)
        {
            XmlSerializer x = new XmlSerializer(t);
            FileStream fs = new FileStream(file, FileMode.Open);
            Object obj = x.Deserialize(fs);

            if (obj.GetType() != t)
            { throw new Exception("Not the same type"); }

            fs.Close();

            return obj;
        }

        public static object DeserializeFromXML(Type t, byte[] msba)
        {
            XmlSerializer x = new XmlSerializer(t);
            MemoryStream ms = new MemoryStream(msba, false);
            Object obj = x.Deserialize(ms);

            if (obj.GetType() != t)
            { throw new Exception("Not the same type"); }

            ms.Close();

            return obj;
        }

        /// <summary>
        /// de: Ändert die Größe eines Bitmap so, dass das Kanal-Logo genau 128 x 128 Pixel hat
        /// en: Changes the size of a bitmap so the channel logo has a exact size of 128 x 128 pixels
        /// </summary>
        /// <param name="bmp">original bitmap (full resolution)</param>
        /// <returns></returns>
        public static Bitmap ResizePicture(Bitmap bmp)
        {
            if (bmp == null) return new Bitmap(128, 128);

            int newWidth = 128, newHeight = 128, startWidth = 0, startHeight = 0;
            double ratio = 0;
            if (bmp.Width > bmp.Height) // Nach Breite ausrichten
            {
                ratio = (double)bmp.Width / (double)128;
                newHeight = (int)Math.Round(bmp.Height / ratio);
                startHeight = (128 - newHeight) / 2;
            }
            else // Nach Höhe ausrichten
            {
                ratio = (float)bmp.Height / (float)128;
                newWidth = (int)Math.Round(bmp.Width / ratio);
                startWidth = (128 - newWidth) / 2;
            }

            Bitmap result = new Bitmap(128, 128);
            using (Graphics g = Graphics.FromImage((Image)result))
            {
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.DrawImage(bmp, startWidth, startHeight, newWidth, newHeight);
            }
            return result;
        }
    }
}
